﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.Auth
{
    public partial class YongHuBianJi : BaseForm
    {
        public YongHuBianJi()
        {
            InitializeComponent();
        }

        private DataGridViewRow m_SelectRow = null;
        public YongHuBianJi(DataGridViewRow SelectRow)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            try
            {
                //保存
                if (string.IsNullOrEmpty(txt_yonghuming.Text) || string.IsNullOrEmpty(txt_mimaqueren.Text) || string.IsNullOrEmpty(txt_mima.Text) || string.IsNullOrEmpty(txt_dengluming.Text))
                {
                    return;
                }
                if (cmb_yonghujuese.SelectedIndex == -1)
                {
                    return;
                }
                if (txt_mima.Text != txt_mimaqueren.Text)
                {
                    KeaoUtils.ShowInfoMessage("密码与密码确认不一致，请重试。");
                    return;
                }

                if (m_SelectRow == null)
                {
                    string CreateAndCondition = KeaoUtils._DB.CreateAndCondition("yonghudengluming", DataValueCommonType.字符串, txt_dengluming.Text, "", SearchConditionType.等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yonghu", CreateAndCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("用户登录名称已存在，请重试设置。");
                        return;
                    }

                    TableRow newRow = new TableRow("tbl_yonghu", KeaoUtils.GetGUID(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "yonghuxingming", ValueMember = txt_yonghuming.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "yonghudengluming", ValueMember = txt_dengluming.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "yonghumima", ValueMember = KeaoUtils.Encrypt(txt_mima.Text), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "yonghujuese", ValueMember = cmb_yonghujuese.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    string CreateAndCondition = KeaoUtils._DB.CreateAndCondition("yonghudengluming", DataValueCommonType.字符串, txt_dengluming.Text, "", SearchConditionType.等于);
                    CreateAndCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yonghu", CreateAndCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("用户登录名称已存在，请重试设置。");
                        return;
                    }
                    TableRow editRow = new TableRow("tbl_yonghu", string.Format("{0}", m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "yonghuxingming", ValueMember = txt_yonghuming.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "yonghudengluming", ValueMember = txt_dengluming.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "yonghumima", ValueMember = KeaoUtils.Encrypt(txt_mima.Text), ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "yonghujuese", ValueMember = cmb_yonghujuese.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                    

                }
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("保存用户信息异常。#{0}", err.Message));
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            //关闭
            this.Close();
        }

        private void YongHuBianJi_Load(object sender, EventArgs e)
        {
            BindInfo();
            if (m_SelectRow != null)
            {
                txt_yonghuming.Text = string.Format("{0}",m_SelectRow.Cells["col_yonghuxingming"].Value);
                txt_dengluming.Text = string.Format("{0}", m_SelectRow.Cells["col_yonghudengluming"].Value);
                txt_mima.Text = KeaoUtils.Decrypt(string.Format("{0}", m_SelectRow.Cells["col_yonghumima"].Value));
                txt_mimaqueren.Text = txt_mima.Text;
                cmb_yonghujuese.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_yonghujuese"].Value);
            }
        }

        private void BindInfo()
        {
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese");
            cmb_yonghujuese.DisplayMember = "juesemingcheng";
            cmb_yonghujuese.ValueMember = "id";
            cmb_yonghujuese.DataSource = Dt;
        }
    }
}
